package com.itheima.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.common.BaseContext;
import com.itheima.common.GlobalConstant;
import com.itheima.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 过滤器
 *
 * @author hua
 * @since 2022/11/1 9:58
 */
@WebFilter(filterName = "loginCheckFilter", urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
    private static  final String notlogin="NOTLOGIN";
    //路径匹配器
    private static final AntPathMatcher antpathMatcher = new AntPathMatcher();
    //2.判断本次请求是否需要校验用户登陆状态
    private static final String[] uris = new String[]{
            "/employee/login",
            "/employee/logout",
            "/backend/**",
            "/front/**",
            "/common/**",//8).文件上传和下载的注解:在检查过滤器中,放行/common/**这个URI
            "/user/sendMsg",//发送验证码
            "/user/login" //用户登录
//            "/favicon.ico"


    };
    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        log.info("已到达过滤器");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //1.获取本次请求的URI
        String requestURI = request.getRequestURI();
        log.info("[拦截请求:{}]", response);
        log.info(requestURI);
        //3.如果不需要处理,则直接放行
        for (String item : uris) {
            //第一次就是匹配的路径,第二次参数请求路径
            boolean match = antpathMatcher.match(item, requestURI);
            if (match) {
                //放行操作
                filterChain.doFilter(request, response);
                return;
            }
        }
        //4.判断登陆状态,如果已登录,则直接放行
       Long currentUserId =(Long) request.getSession().getAttribute(GlobalConstant.EMPLOYEE_ID);
        //如果不为空,代表已经登录
        if (currentUserId != null) {
            //已登录,直接放行
            //方形操作
            //当前执行线程ID
            long threadId = Thread.currentThread().getId();
            log.info("过滤器 ===> 当前执行ID ===> {}",threadId);
            //存入threadlocal
            BaseContext.setCurrentUserId(currentUserId);
            filterChain.doFilter(request, response);
            return;
        }
        // 兼容移动端用户
        Long mobileId = (Long) request.getSession().getAttribute(GlobalConstant.MOBILE_ID);
// 如果不是空,就代表已经登录
        if (mobileId != null) {
            // 已登录，则直接放行
            // 放行操作
            // 当前执行线程ID
            long threadId = Thread.currentThread().getId();
            log.info("过滤器====当前执行线程ID ====> {}", threadId);
            // 存入threadlocal
            // 写入的id 也要是移动端用户的id
            BaseContext.setCurrentUserId(mobileId);
            filterChain.doFilter(request, response);
            return;
        }
        //5.如果未登录则返回未登录结果
        R<String> notlogin = R.error("NOTLOGIN");
        response.getWriter().write(JSON.toJSONString(notlogin));
    }

}
